package com.itheima.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.excption.MyException;
import com.itheima.pojo.PmsProduct;
import com.itheima.pojo.PmsProductCategory;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsProductCategoryService {
    @DubboReference
    private PmsProductCategoryApi categoryApi;
    @DubboReference
    private PmsProductApi productApi;

    public void save(PmsProductCategory category) {
        if (ObjectUtil.isEmpty(category)) throw new MyException("正确输入分类");
        if (category.getParentId() == 0) category.setLevel(0);
        else category.setLevel(1);
        categoryApi.save(category);
    }

    public void delete(Long id) {
        PmsProductCategory category = categoryApi.findById(id);
        if (ObjectUtil.isEmpty(category)) throw new MyException("没有此分类");
        List<PmsProduct> pmsProducts = productApi.findAll();
        for (PmsProduct pmsProduct : pmsProducts) {
            if (pmsProduct.getProductCategoryId() == category.getId()) throw new MyException("该分类下还有商品,不能删除");
        }
        List<PmsProductCategory> categories = categoryApi.findAll();
        for (PmsProductCategory caregory : categories) {
            if (caregory.getParentId() == category.getId()) throw new MyException("该分类下还有子分类,不能删除");
        }
        categoryApi.remove(id);
    }

    public List<PmsProductCategoryVo> getAllCategory() {
        List<PmsProductCategory> categories = categoryApi.findByLevel(0);
        List<PmsProductCategoryVo> vos = new ArrayList<>();
        for (PmsProductCategory category : categories) {
            PmsProductCategoryVo vo = new PmsProductCategoryVo();
            List<PmsProductCategory> categoryList = categoryApi.findByPid(category.getId());
            BeanUtil.copyProperties(category, vo);
            vo.setChildren(categoryList);
            vos.add(vo);
        }
        return vos;
    }

    public PageResultVo getPages(Long pid, Integer pageNum, Integer pageSize) {
        PageResultVo vo = categoryApi.getPages(pid, pageNum, pageSize);
        return vo;
    }

    //修改navStatus,并查看该等级下是否有其他分类,并修改子分类状态
    public void updateNavStatus(Integer ids, Integer navStatus) {
        categoryApi.updateNavStatus(ids, navStatus);
        PmsProductCategory category = categoryApi.getByid(Long.valueOf(ids));
        if (category.getParentId() == 0) {
            //找到子分类,并修改子分类状态
            List<PmsProductCategory> categoryList = categoryApi.findByPid(category.getId());
            if (CollUtil.isEmpty(categoryList))return;
            for (PmsProductCategory pmsProductCategory : categoryList) {
                pmsProductCategory.setNavStatus(navStatus);
                categoryApi.updateById(pmsProductCategory.getId(), pmsProductCategory);
            }
        }
    }

    public void updateShowStatus(Integer ids, Integer showStatus) {
        categoryApi.updateShowStatus(ids, showStatus);
        PmsProductCategory category = categoryApi.getByid(Long.valueOf(ids));
        if (category.getParentId() == 0) {
            //找到子分类,并修改子分类状态
            List<PmsProductCategory> categoryList = categoryApi.findByPid(category.getId());
            if (CollUtil.isEmpty(categoryList))return;
            for (PmsProductCategory pmsProductCategory : categoryList) {
                pmsProductCategory.setShowStatus(showStatus);
                categoryApi.updateById(pmsProductCategory.getId(), pmsProductCategory);
            }
        }
    }

    public void updateById(Long id, PmsProductCategory category) {
        categoryApi.updateById(id, category);
    }

    public PmsProductCategory getById(Long id) {
        return categoryApi.getByid(id);
    }
}
